package com.yztc.yidoutang.activity.qwj_detail_activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.yztc.yidoutang.R;
import com.yztc.yidoutang.base.BaseActivity;
import com.yztc.yidoutang.bean.QuanWuDetailListBean;
import com.yztc.yidoutang.utils.GlideCircleTransform;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import cn.sharesdk.onekeyshare.OnekeyShare;

/**
 * Created by My on 2016/10/24.
 */

public class QuanWuDetailActivity extends BaseActivity implements QuanWuDetailContract.View {
    private Toolbar mToobar;
    private WebView wv;
    private TextView tv_userName,tv_title,tv_style,tv_size,tv_area,tv_budget,tv_content;
    private ImageView iv_user,iv_title;
    private QuanWuDetailListBean bean;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent intent=getIntent();
        String id=intent.getStringExtra("id");
        QuanWuDetailPresenter presenter=new QuanWuDetailPresenter(this,bean);
        presenter.getDataForNet(id);
        initView();
    }

    @SuppressWarnings("ResourceAsColor")
    private void initView() {
        tv_userName= (TextView) findViewById(R.id.qwj_detail_tv_username);
        tv_title= (TextView) findViewById(R.id.qwj_detail_tv_title);
        tv_size= (TextView) findViewById(R.id.qwj_detail_tv_size);
        tv_style= (TextView) findViewById(R.id.qwj_detail_tv_style);
        tv_area= (TextView) findViewById(R.id.qwj_detail_tv_area);
        tv_budget= (TextView) findViewById(R.id.qwj_detail_tv_budget);
        iv_user= (ImageView) findViewById(R.id.qwj_detail_iv_user);
        iv_title= (ImageView) findViewById(R.id.qwj_detail_iv_title);
        wv= (WebView) findViewById(R.id.qwj_detail_tv_content);
        setSupportActionBar(mToobar);


    }

    @Override
    public void onQuanWuDetailSuccess(QuanWuDetailListBean bean) {
        tv_userName.setText(bean.getUserName());
        tv_title.setText(bean.getTitle());
        tv_size.setText(bean.getSize());
        tv_style.setText(bean.getStyle());
        tv_area.setText(bean.getArea());
        tv_budget.setText(bean.getBudget());
        wv.getSettings().setDefaultTextEncodingName("UTF-8");
        //WebView双击变大，再双击后变小，当手动放大后，双击可以恢复到原始大小
        wv.getSettings().setUseWideViewPort(true);
        wv.getSettings().setLoadWithOverviewMode(true);
        //支持缩放
        wv.getSettings().setJavaScriptEnabled(true);
        wv.getSettings().setBuiltInZoomControls(true);
        wv.getSettings().setSupportZoom(true);
        wv.getSettings().setTextSize(WebSettings.TextSize.LARGEST);
        wv.loadDataWithBaseURL(null,getNewContent(bean.getContent()),"text/html","UTF-8",null);
        Glide.with(this).load(bean.getUserPic()).transform(new GlideCircleTransform(this)).into(iv_user);
        Glide.with(this).load(bean.getCover()).placeholder(R.mipmap.ic_default_load).into(iv_title);
        Log.i("tag","--->"+bean.getArea()+bean.getBudget()+bean.getSize()+bean.getStyle()+bean.getContent());

    }
    private String getNewContent(String htmltext){

        Document doc= Jsoup.parse(htmltext);
        Elements elements=doc.getElementsByTag("img");
        for (Element element : elements) {
            element.attr("width","100%").attr("height","auto");
        }

        Log.d("VACK", doc.toString());
        return doc.toString();
    }




    @Override
    public void onQuanWuDetailFail(String msg) {

    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_scrolling;
    }
    public void click(View view){
        switch (view.getId()){
            case R.id.qwj_detail_btn_back:
                finish();
                break;
            case R.id.qwj_detail_share:
                OnekeyShare onekeyShare=new OnekeyShare();
                onekeyShare.disableSSOWhenAuthorize();//关闭sso授权
                //设置一键分享的内容
                onekeyShare.setTitle("一键分享的标题");
                onekeyShare.setText("一键分享的内容");
                onekeyShare.setImageUrl("http://img4.imgtn.bdimg.com/it/u=2291132731,375716043&fm=21&gp=0.jpg");
                onekeyShare.show(this);
                break;
        }
    }
}
